﻿using PhuLeBussinessObject.Core;
using PhuLeBooze.Global;
using PhuLeBussinessObject.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

namespace PhuLeBooze.Views.Core.Service
{
    /// <summary>
    /// Summary description for ListDataService
    /// </summary>
    public class ListDataService : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            CoreUI coreUI = new CoreUI();
            string action = string.Empty;
            if (context.Request["Action"] != null)
            {
                action = context.Request["Action"];
            }
            string output = string.Empty;
            string inputValue = GetInputValue(context);
            switch (action)
            {
                case "INSERT":
                case "UPDATE":
                case "DELETE":
                    output = UpdateListData(inputValue);
                    break;
                default:
                    int numberOfRows = Convert.ToInt32(context.Request["rows"]);
                    int pageIndex = Convert.ToInt32(context.Request["page"]);
                    output = GetListDataGrid(inputValue, numberOfRows, pageIndex);
                    break;
            }
            context.Response.Write(output);
        }
        protected string GetInputValue(HttpContext context)
        {
            return new CCore().GetInputValue(context);
        }
        protected string GetListDataGrid(string inputValue, int numberOfRows, int pageIndex)
        {
            HttpContext context = HttpContext.Current;

            CGrid.JQGridResults result = new CCore().GetListDataGrid(inputValue, numberOfRows, pageIndex);
            return new JavaScriptSerializer().Serialize(result);
        }
        protected string UpdateListData(string inputValue)
        {
            CApplicationMessage result = new CCore().UpdateListData(inputValue);
            return new JavaScriptSerializer().Serialize(result);
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}